﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class FileHelper
    {
        /// <summary>
        /// 获取文件的MD5值
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetMd5(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }

        /// <summary>
        /// 获取相对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetRelativePath(string path)
        {
            var current_path = Directory.GetCurrentDirectory();
            return GetRelativePath(path, current_path);
        }

        /// <summary>
        /// 获取相对路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="parent_path"></param>
        /// <returns></returns>
        public static string GetRelativePath(string path, string parent_path)
        {
            if (path.StartsWith(parent_path))
            {
                return path.Replace(parent_path, "").TrimStart('\\');
            }

            return path;
        }

        /// <summary>
        /// 获取全部的子文件的路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="parent_list"></param>
        /// <returns></returns>
        public static IEnumerable<string> GetAllChildFilePath(string path)
        {
            List<string> file_list = new List<string>();
            try
            {
                file_list.AddRange(Directory.GetFiles(path));

                foreach (var item in Directory.GetDirectories(path))
                {
                    file_list.AddRange(GetAllChildFilePath(item));
                }
            }
            catch (Exception ex)
            {

            }
            return file_list;
        }

        public static void RemoveAllChild(string filepath)
        {
            if (!Directory.Exists(filepath))
                return;

            var all_file = GetAllChildFilePath(filepath);
            foreach (var item in all_file)
            {
                try
                {
                    File.Delete(item);
                }
                catch (Exception ex)
                {

                }
            }
        }
    }
}
